package com.neo.blog.aop;
 
import java.util.Arrays;

import javax.servlet.http.HttpServletRequest; 
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

///切面 记录日志
@Aspect
@Component
public class WebLogAspect {

	private final static Logger logger = LoggerFactory.getLogger(WebLogAspect.class);

	// ..表示包及子包 该方法代表controller层的所有方法
	@Pointcut("execution(public * com.neo.blog.controller..*.*(..))")
	public void controllerMethod() {
	}
	@Before("controllerMethod()")
	public void LogRequestInfo(JoinPoint joinPoint)throws Exception {
		//接收到请求，记录请求内容
		 ServletRequestAttributes attributes=(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
		 HttpServletRequest request=attributes.getRequest();
		 //记录请求的内容
		 String url="URL:"+request.getRequestURI();
		 String mhd="Method_Name:"+joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
		 String args="args:"+Arrays.toString(joinPoint.getArgs());
		 logger.info(url+mhd);
	}
	
	@AfterReturning(returning="ret",pointcut="controllerMethod()")
	public void doAfterReturning(Object ret) throws Exception {
        // 处理完请求，返回内容
        logger.info("RESPONSE : " + ret);
	}

}
